<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" width="70vw" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" draggable overflow>
        <el-container>
            <el-aside width="200px" v-loading="showGrouploading" style="height: 600px;">
                <el-container>
                    <el-header>
                        <el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
                    </el-header>
                    <el-main class="nopadding">
                        <el-tree ref="group" class="menu" node-key="id" :data="cateList" :props="cateProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
                    </el-main>
                </el-container>
            </el-aside>
            <el-container>
                <el-header>
                    <div class="left-panel">
                        <el-button type="primary" @click="back">确定</el-button>
                        <el-button type="primary" icon="el-icon-plus" v-auth="'goods.add'" @click="add">新增</el-button>
                    </div>
                    <div class="right-panel">
                        <div class="right-panel-search">
                            <el-input v-model="search.keyword" placeholder="关键词搜索" clearable @keyup.enter="upsearch"></el-input>
                            <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                            <el-button type="primary" @click="unsetSearch">重置</el-button>
                        </div>
                    </div>
                </el-header>
                <el-main class="nopadding">
                    <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border height="650" :params="{system_disable:0}">
                        <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                        <el-table-column type="index" label="" fixed align="center" width="50"></el-table-column>
                        <el-table-column label="分类名称" prop="cate.apply_cate_name" align="center"></el-table-column>
                        <el-table-column label="供应商编号" prop="apply_code" align="center"></el-table-column>
                        <el-table-column label="供应商名称" prop="apply_name" align="center"></el-table-column>
                        <el-table-column label="供应商助记码" prop="help_remember" align="center"></el-table-column>
                        <el-table-column label="应付欠款" prop="only_money" align="center"></el-table-column>
                        <!-- <el-table-column label="期初预付(元)" prop="amount" align="center"></el-table-column> -->
                        <el-table-column label="联系人" prop="linkman" align="center"></el-table-column>
                        <el-table-column label="联系电话" prop="mobile" align="center"></el-table-column>
                        <el-table-column label="联系地址" prop="address" align="center"></el-table-column>
                        <el-table-column label="简介" prop="desc" align="center"></el-table-column>
                        <el-table-column label="添加时间" prop="create_time" align="center"></el-table-column>
                    </scTable>
                </el-main>
            </el-container>
        </el-container>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
        </template>
        <save-add v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-add>
    </el-dialog>
</template>

<script>
import saveAdd from './save.vue'

export default {
    components: {saveAdd},
    emits: ['success', 'closed'],
    data() {
        return {
        mode: "add",
        titleMap: {
            add: '选择供应商',
            edit: '选择供应商',
            show: '查看'
        },
        visible: false,
        isSaveing: false,
        showGrouploading: false,
        groupFilterText: '',
        cateList: [],
        cateProps: {
            label: (data) => {
            return data.apply_cate_name
            }
        },
        apiObj: this.$API.apply.apply.lists,
        selection: [],
        search: {
            keyword: null,
        },
        dialog: {
            save: false,
        },
        }
    },
    watch: {
        groupFilterText(val) {
        this.$refs.group.filter(val);
        }
    },
    mounted() {
        this.getGroup()
    },
    methods: {
        // 显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        // 表单注入数据
        setData(data) {
            this.type = data
        },
        // 选择事件
        selectionChange(selection) {
            this.selection = selection;
        },
        // 选中返回
        back() {
            this.$emit('success', this.selection, this.type)
            this.visible = false;
        },
        //商品新增
        add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('add')
            })
        },
        // 更新商品新增
        handleSuccess() {
            this.$refs.table.refresh()
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
        //加载树数据
        async getGroup() {
            this.showGrouploading = true;
            var res = await this.$API.apply.applyCate.all.get();
            this.showGrouploading = false;
            var allNode = { apply_cate_id: '', apply_cate_name: '所有' }
            res.data.unshift(allNode);
            this.cateList = res.data;
        },
        //树过滤
        groupFilterNode(value, data) {
            if (!value) return true;
            return data.apply_cate_name.indexOf(value) !== -1;
        },
        //树点击事件
        groupClick(data) {
            var params = {
                apply_cate: data.apply_cate_id
            }
            this.$refs.table.reload(params)
        },
    }
}
</script>

<style></style>
